package cn.dong.ucenter.controller.api;


import cn.dong.api.domain.CommonResult;
import cn.dong.api.domain.ResultCodeEnum;
import cn.dong.api.utils.JwtInfo;
import cn.dong.api.utils.JwtUtils;
import cn.dong.base.dto.MemberDto;
import cn.dong.base.dto.TradeOrderDto;
import cn.dong.base.handler.CustomException;
import cn.dong.ucenter.domain.dto.LoginVo;
import cn.dong.ucenter.domain.dto.MemberBasicVo;
import cn.dong.ucenter.domain.dto.MemberRegisterVo;
import cn.dong.ucenter.service.MemberService;
import com.netflix.client.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-18
 */
@RestController
@RequestMapping("/api/ucenter/member")
@Slf4j
public class ApiMemberController {

    @Autowired
    private MemberService memberService;


    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody MemberRegisterVo memberRegisterVo) throws ClientException {
        memberService.register(memberRegisterVo);
        return CommonResult.ok();
    }

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        return CommonResult.ok().data(token).message("登录成功！");
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @PostMapping("/getInfo")
    public CommonResult getInfo(HttpServletRequest request) {
        // 检验请求头信息带来的token
        JwtInfo info = null;
        try {
            info = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            log.error("用户信息解析失败：" + e.getMessage());
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }

        return CommonResult.ok().data(info);

    }

    /**
     * 根据会员id，查询会员信息，用于订单表中的会员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/inner/member/getMemberById/{id}")
    public MemberDto getMemberById(@PathVariable String id) {
        return memberService.getMemberById(id);
    }

    /**
     * 查询用户的基本资料
     *
     * @return
     */
    @GetMapping("/auth/getBasicInfo}")
    public CommonResult getBasicInfo(HttpServletRequest request) {
        JwtInfo token = JwtUtils.getMemberIdByJwtToken(request);
        if (token == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        MemberBasicVo mbv = memberService.getBasicInfo(token.getId());
        return CommonResult.ok().data(mbv);
    }


    /**
     * 查询这个用户的订单，（根据用户查询全部收藏，在edu服务中）
     *
     * @return
     */
    @GetMapping("/auth/getOrderAll")
    public CommonResult getOrderAll(HttpServletRequest request) {
        JwtInfo token = JwtUtils.getMemberIdByJwtToken(request);
        if (token == null) {
            throw new CustomException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        List<TradeOrderDto> orderAll = memberService.getOrderAll(token.getId());
        return CommonResult.ok().data(orderAll);
    }


}

